package cn.edu.jxnu.sse.dao;


import cn.edu.jxnu.sse.entity.Book;
import cn.edu.jxnu.sse.utils.DBUtil;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class BookDao {
    public List<Book> findByName(String name){
        List<Book> ret = new ArrayList<Book>();
        Connection conn = DBUtil.getConnection();
        try {
            PreparedStatement st = conn.prepareStatement("select * from bookt where book_Name like ?");
            st.setString(1,"%"+name+"%");
            ResultSet rst = st.executeQuery();
            while(rst.next()){
                Book book=new Book();
                book.setBookName(rst.getString("book_Name"));
                book.setBookAuthor(rst.getString("book_Author"));
                book.setBookId(rst.getInt("book_ID"));
                book.setPrice(rst.getDouble("price"));
                ret.add(book);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return ret;
    }
}
